<template>
  <div class="dashboard">
    <!-- 侧边栏 -->
    <aside class="sidebar">
      <div class="logo">
        <h2>🏪 商家管理</h2>
      </div>
      
      <nav class="menu">
        <router-link to="/dashboard" class="menu-item" active-class="active">
          <i class="material-icons">dashboard</i>
          <span>仪表板</span>
        </router-link>
        <router-link to="/shop-info" class="menu-item" active-class="active">
          <i class="material-icons">store</i>
          <span>店铺信息</span>
        </router-link>
        <router-link to="/foods" class="menu-item" active-class="active">
          <i class="material-icons">restaurant_menu</i>
          <span>菜品管理</span>
        </router-link>
        <router-link to="/orders" class="menu-item" active-class="active">
          <i class="material-icons">shopping_cart</i>
          <span>订单管理</span>
        </router-link>
        <router-link to="/statistics" class="menu-item" active-class="active">
          <i class="material-icons">bar_chart</i>
          <span>数据统计</span>
        </router-link>
        <router-link to="/wallet" class="menu-item" active-class="active">
          <i class="material-icons">account_balance_wallet</i>
          <span>商家钱包</span>
        </router-link>
      </nav>
      
      <div class="user-info">
        <p>{{ businessName }}</p>
        <button @click="handleLogout" class="logout-btn">
          <i class="material-icons">logout</i>
          退出登录
        </button>
      </div>
    </aside>
    
    <!-- 主内容区 -->
    <main class="main-content">
      <header class="top-bar">
        <h1>仪表板</h1>
        <div class="user-menu">
          <span>{{ businessName }}</span>
          <i class="material-icons">account_circle</i>
        </div>
      </header>
      
      <div class="content">
        <div class="stats-grid">
          <div class="stat-card">
            <i class="material-icons">shopping_cart</i>
            <div class="stat-info">
              <p class="stat-label">今日订单</p>
              <p class="stat-value">{{ stats.todayOrders }}</p>
            </div>
          </div>
          
          <div class="stat-card">
            <i class="material-icons">attach_money</i>
            <div class="stat-info">
              <p class="stat-label">今日收入</p>
              <p class="stat-value">¥{{ formatMoney(stats.todayRevenue) }}</p>
            </div>
          </div>
          
          <div class="stat-card">
            <i class="material-icons">restaurant_menu</i>
            <div class="stat-info">
              <p class="stat-label">菜品数量</p>
              <p class="stat-value">{{ stats.foodCount }}</p>
            </div>
          </div>
          
          <div class="stat-card">
            <i class="material-icons">star</i>
            <div class="stat-info">
              <p class="stat-label">店铺评分</p>
              <p class="stat-value">{{ stats.rating }}</p>
            </div>
          </div>
        </div>
        
        <div class="quick-actions">
          <h2>快速操作</h2>
          <div class="action-buttons">
            <router-link to="/shop-info" class="action-btn">
              <i class="material-icons">edit</i>
              编辑店铺信息
            </router-link>
            <router-link to="/foods" class="action-btn">
              <i class="material-icons">add</i>
              添加菜品
            </router-link>
            <router-link to="/orders" class="action-btn">
              <i class="material-icons">list</i>
              查看订单
            </router-link>
            <router-link to="/comments" class="action-btn">
              <i class="material-icons">rate_review</i>
              查看评价
            </router-link>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
export default {
  name: 'Dashboard',
  data() {
    return {
      stats: {
        todayOrders: 0,
        todayRevenue: '0.00',
        foodCount: 0,
        rating: 0,
        businessName: '',
        walletBalance: '0.00'
      }
    }
  },
  mounted() {
    this.loadDashboardData()
  },
  computed: {
    businessName() {
      return this.$store.state.businessName || '商家'
    }
  },
  methods: {
    async loadDashboardData() {
      try {
        const businessId = this.$store.state.businessId
        if (!businessId) {
          this.$router.push('/login')
          return
        }

        const response = await this.$axios.post('/elm/BusinessManagementController/getDashboardStats', null, {
          params: { businessId }
        })

        if (response.data && (response.data.code === 200 || response.data.code === '200')) {
          const result = response.data.result
          this.stats = {
            todayOrders: result.todayOrders || 0,
            todayRevenue: result.todayRevenue || '0.00',
            foodCount: result.foodCount || 0,
            rating: result.rating || 0,
            businessName: result.businessName || '',
            walletBalance: result.walletBalance || '0.00'
          }
          console.log('仪表板数据加载成功:', this.stats)
        } else {
          console.error('获取仪表板数据失败:', response.data?.message)
          alert('获取仪表板数据失败，请重试')
        }
      } catch (error) {
        console.error('加载仪表板数据失败:', error)
        alert('加载仪表板数据失败，请检查网络连接')
      }
    },

    // 格式化金额显示
    formatMoney(amount) {
      if (!amount) return '0.00'
      const num = parseFloat(amount)
      return num.toFixed(2)
    },

    handleLogout() {
      this.$store.dispatch('logout')
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.dashboard {
  display: flex;
  height: 100vh;
  background-color: #f5f5f5;
}

.sidebar {
  width: 250px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
}

.logo {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.logo h2 {
  font-size: 20px;
  margin: 0;
}

.menu {
  flex: 1;
  padding: 20px 0;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  transition: all 0.3s;
  border-left: 3px solid transparent;
}

.menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: white;
}

.menu-item.active {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  border-left-color: white;
}

.menu-item i {
  margin-right: 12px;
}

.user-info {
  padding: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}

.user-info p {
  margin: 0 0 10px 0;
  font-size: 14px;
}

.logout-btn {
  width: 100%;
  padding: 8px;
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 5px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  transition: all 0.3s;
}

.logout-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.top-bar {
  background: white;
  padding: 20px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.top-bar h1 {
  margin: 0;
  font-size: 24px;
  color: #333;
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #666;
}

.content {
  flex: 1;
  overflow-y: auto;
  padding: 30px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.stat-card {
  background: white;
  border-radius: 10px;
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 15px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-card i {
  font-size: 32px;
  color: #667eea;
}

.stat-info {
  flex: 1;
}

.stat-label {
  margin: 0;
  color: #999;
  font-size: 12px;
}

.stat-value {
  margin: 5px 0 0 0;
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

.quick-actions {
  background: white;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.quick-actions h2 {
  margin: 0 0 15px 0;
  font-size: 16px;
  color: #333;
}

.action-buttons {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 15px;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 15px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 8px;
  text-decoration: none;
  transition: transform 0.3s;
  gap: 8px;
}

.action-btn:hover {
  transform: translateY(-3px);
}

.action-btn i {
  font-size: 24px;
}
</style>
